import request from '@/utils/request'
import { RootThunkAction } from '@/types/store'
import { ApiResponse, LoginAction, LoginForm, Token } from '@/types/data'
import { removeToken, setToken } from '@/utils/storage'
export const login = (data:LoginForm):RootThunkAction => {
  return async (dispatch) => {
    const res = await request.post<ApiResponse<Token>>('/authorizations', data)

    // 存到本地
    setToken(res.data.data)

    // 存到redux
    dispatch({
      type: 'login/token',
      payload: res.data.data
    })
  }
}
// 获取验证码
export const getCode = (mobile: string) => {
  return async () => {
    await request.get(`/sms/codes/${mobile}`)

    // 注意：验证码是发送到手机上的，因此，不需要更新Redux状态
  }
}
export const logout = ():LoginAction => {
  removeToken()
  return {
    type: 'login/logout'
  }
}
